package pro.shushi.pamirs.hr.core.action.eam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pro.shushi.pamirs.framework.connectors.data.tx.transaction.Tx;
import pro.shushi.pamirs.hr.api.enums.eam.EamModelSrcEnum;
import pro.shushi.pamirs.hr.api.enums.eam.UkDevStatEnum;
import pro.shushi.pamirs.hr.api.model.eam.BsMtoolMd;
import pro.shushi.pamirs.hr.api.model.eam.EamMtoolMtPj;
import pro.shushi.pamirs.hr.api.pmodel.eam.EamUkDtMtoolProxy;
import pro.shushi.pamirs.hr.api.pmodel.eam.EamUkHdMtoolProxy;
import pro.shushi.pamirs.hr.api.service.eam.EamUkHdMtoolService;
import pro.shushi.pamirs.hr.api.tmodel.EamUkDtMtoolReq;
import pro.shushi.pamirs.hr.api.tmodel.EamUkHdMtoolReq;
import pro.shushi.pamirs.meta.annotation.Action;
import pro.shushi.pamirs.meta.annotation.Function;
import pro.shushi.pamirs.meta.annotation.Model;
import pro.shushi.pamirs.meta.annotation.fun.extern.Slf4j;
import pro.shushi.pamirs.meta.api.dto.config.TxConfig;
import pro.shushi.pamirs.meta.enmu.ActionContextTypeEnum;
import pro.shushi.pamirs.meta.enmu.FunctionOpenEnum;
import pro.shushi.pamirs.meta.enmu.FunctionTypeEnum;
import pro.shushi.pamirs.meta.enmu.ViewTypeEnum;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 工装型号自定义
 */
@Slf4j
@Component
@Model.model(EamUkHdMtoolProxy.MODEL_MODEL)
public class EamUkHdMtoolProxyAction {

    @Autowired
    private EamUkHdMtoolService eamUkHdMtoolService;

    @Function(openLevel = FunctionOpenEnum.API, summary = "选择工装联动")
    @Function.Advanced(displayName = "选择工装联动", type = FunctionTypeEnum.QUERY)
    public EamUkHdMtoolProxy constructBsMtoolChange(EamUkHdMtoolProxy data) {
        if (data.getBsMtool()==null) {
            return data;
        }

        String mmdCode = data.getBsMtool().getMmdCode();
        String orgCode = data.getBsMtool().getOrgCode();
        BsMtoolMd bsMtoolMd = new BsMtoolMd().setOrgCode(orgCode).setMmdCode(mmdCode).queryOne();
        data.setBsMtoolMd(bsMtoolMd);

        data.setRefCode(data.getBsMtool().getMtoolCode());
        data.setEamModelCode(mmdCode);
        //写死来源
        data.setEamModelSrc(EamModelSrcEnum.MMD);

        if(data.getUkCycType() == null){
            return data;
        }
        //按保养类型和型号带出保养项 -- 注意只有新增的时候选择工装带出 TODO
        List<EamUkDtMtoolProxy> eamUkDtList= new ArrayList<>(16);
        List<EamMtoolMtPj> mtoolMtPjList = new EamMtoolMtPj().setOrgCode(orgCode)
                        .setMmdCode(mmdCode).setUkCycType(data.getUkCycType()).queryList();
        if(mtoolMtPjList != null && mtoolMtPjList.size()>0){
            int seq = 1;
            for(EamMtoolMtPj pjRow : mtoolMtPjList){
                EamUkDtMtoolProxy ukDt = new EamUkDtMtoolProxy();
                ukDt.setOrgCode(orgCode);
                ukDt.setUkCycType(data.getUkCycType());
                ukDt.setSeq(seq);
                ukDt.setPjCode(pjRow.getPjCode());
                ukDt.setPjName(pjRow.getPjName());
                ukDt.setPjType(pjRow.getMtoolPjType());
                ukDt.setUkMtd(pjRow.getUkMtd());
                ukDt.setUkStd(pjRow.getUkStd());
                ukDt.setUkDevStat(UkDevStatEnum.UNKONW);
                eamUkDtList.add(ukDt);

                seq++;
            }
        }
        data.setEamUkDtList(eamUkDtList);

        return data;
    }

    /**
     * 开始保养检查
     * @param data
     * @return
     */
    @Action(displayName = "开始保养",bindingType = ViewTypeEnum.TABLE,contextType = ActionContextTypeEnum.SINGLE)
    public void startMaintainCheck(EamUkHdMtoolReq data){
        //开始保养校验
        eamUkHdMtoolService.beforeStartMaintain(data);
    }

    /**
     * 确认开始保养
     * @param data
     * @return
     */
//    @Action(displayName = "确认开始保养",bindingType = ViewTypeEnum.TABLE,contextType = ActionContextTypeEnum.SINGLE_AND_BATCH)
    public void confirmStartMaintain(EamUkHdMtoolReq data){
        //使用注解事务
        eamUkHdMtoolService.confirmStartMaintain(data);
    }

    /**
     * 保养完成
     * @param data
     * @return
     */
    @Action(displayName = "保养完成",bindingType = ViewTypeEnum.FORM,contextType = ActionContextTypeEnum.SINGLE)
    public void completeMaintain(EamUkHdMtoolReq data){
        //保养完成前校验
        eamUkHdMtoolService.beforecompleteMaintain(data);

        //保养完成保存 -- 测试代码事务
        Tx.build(new TxConfig()).executeWithoutResult(status -> {
            //执行逻辑
            eamUkHdMtoolService.completeMaintain(data);
        });
//        eamUkHdMtoolService.completeMaintain(data);
    }

    /**
     * 录入实测值更新保养项
     * @param data
     * @return
     */
    @Function(openLevel = FunctionOpenEnum.API, summary = "录入实测值更新保养项")
    public Map<String, Object> updateUkDtMaintain(EamUkDtMtoolReq data){
        Map<String, Object> result =  eamUkHdMtoolService.updateUkDtMaintain(data);
        return result;
    }

}
